<?php

namespace App\Console\Commands;

use App\Models\Admin\DepositOut;
use App\Models\Admin\RuzhuMerchantBasic;
use App\Models\Admin\Withdraw;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

class CronUnfreezeDeposit extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'unfreeze';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '每日定时解冻';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command. 每天3点5分开始执行
     *
     * @return mixed
     * @throws \Exception
     */
    public function handle()
    {
        error_reporting(E_ERROR);
        $h = date('H');
        $i = date('i');
        if('03' != $h || $i > 10){
            self::log('error time!');
            exit("error time!"); //2017-12-14 10:56:46注释
        }
        set_time_limit(0);
        if(Cache::has('script_cron_unfreeze_deposit')){
            self::log('is running!');
            exit('is running');
        }
        Cache::put('script_cron_unfreeze_deposit' , 1 , 10);//增加10分钟缓存
        $outModel = new DepositOut();
        $withDraw = new Withdraw();
        $tmp=RuzhuMerchantBasic::all(['node_id','deposit100','deposit200','deposit300']);
        foreach ($tmp as $item){
            if(($item['deposit100']+$item['deposit200']+$item['deposit300'])<10){
                self::log('低于10元的不计算!nodeid is '.$item->node_id);
                continue;//低于10元的不计算 2017年12月14日10:54:07注释
            }
            $unfreeze100=$item['deposit100']/100;
            $unfreeze200=$item['deposit200']/200;
            $unfreeze300=$item['deposit300']/300;
            if(($unfreeze100+$unfreeze200+$unfreeze300)<0.0001){
                self::log('低于0.01分的不计算!nodeid is '.$item->node_id);
                continue;//低于1分的不计算 2017-12-14 10:53:55注释
            }
            //算出已解冻金额
            $unfreeze = $outModel->getUnfreeze($item->node_id);
            $can_withdraw = $unfreeze + ($unfreeze100+$unfreeze200+$unfreeze300) - $withDraw->getCashed($item->node_id, 3);//当前可提现金额
            DB::beginTransaction();
            //扣除字段数值
            DB::table('ruzhu_merchant_basic')->where([
                'node_id'=>$item->node_id
            ])->update([
                'deposit100'=>$item['deposit100']-$unfreeze100,
                'deposit200'=>$item['deposit200']-$unfreeze200,
                'deposit300'=>$item['deposit300']-$unfreeze300
            ]);
            //增加记录到解冻表中
            $outModel->insert([
                'id' => DepositOut::getNextSeq(),
                'node_id' => $item->node_id,
                'add_time' => date('Y-m-d H:i:s'),
                'deposit_balance' => $item['deposit100']+$item['deposit200']+$item['deposit300']-($unfreeze100+$unfreeze200+$unfreeze300),
                'unfreeze_balance' => $unfreeze100+$unfreeze200+$unfreeze300,
                'remarks' => '存款金额自动释放',
                'can_withdraw' => $can_withdraw,
            ]);
            DB::commit();
        }
        self::log('OK!');
        exit(1);
    }

    /**
     * 记录日志
     * @param $msg
     */
    public static function log($msg){
        file_put_contents(storage_path('logs/unfreeze.log'),date('Y-m-d H:i:s').PHP_EOL.$msg.PHP_EOL,FILE_APPEND);
    }
}
